package com.yhy.dubbo.service.mongo;

import com.yhy.domain.mongo.Comment;
import com.yhy.domain.mongo.Publish;
import com.yhy.domain.mongo.Video;
import com.yhy.domain.vo.PageResult;
import com.yhy.dubbo.api.mongo.CommentApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment) {
        //确定新增条件 当前用户的userId 此条评论的publishId和评论类型commentType
        //1.需要手动配置的参数
        comment.setCreated(System.currentTimeMillis());

        //2.查询动态
        if (comment.getCommentType() < 4) {//动态 1.点赞 2.评论 3.喜欢
            comment.setId(ObjectId.get());
            Publish publish = publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
            comment.setPublishUserId(publish.getUserId());

            //3.comment所有参数封装完毕,保存到comment表
            mongoTemplate.save(comment);

            //4.让publish表点赞数+1
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(), 1);
            mongoTemplate.updateFirst(query, update, Publish.class);

            publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
            return publish.getCount(comment.getCommentType());
        } else if (comment.getCommentType() == 4) {//对评论点赞
            Comment targetComment = mongoTemplate.findById(comment.getPublishId(), Comment.class);
            comment.setPublishUserId(targetComment.getUserId());
            comment.setId(ObjectId.get());
            mongoTemplate.save(comment);
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(), 1);
            mongoTemplate.updateFirst(query, update, Comment.class);
            targetComment = mongoTemplate.findById(comment.getPublishId(), Comment.class);
            return targetComment.getLikeCount();
        }else if(comment.getCommentType()==5){//视频点赞
//            Comment likeComment = new Comment();
//            likeComment.setUserId(null);
            Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
            comment.setPublishUserId(video.getUserId());
            //保存评论
            mongoTemplate.save(comment);
            //修改视频的点赞数并返回点赞数+1
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc("likeCount",1);
            mongoTemplate.updateFirst(query,update,Video.class);

            video = mongoTemplate.findById(comment.getPublishId(),Video.class);
            return video.getLikeCount();
        }else if(comment.getCommentType()==6){//视频评论
            Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
            comment.setPublishUserId(video.getUserId());

            mongoTemplate.save(comment);

            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc("commentCount",1);
            mongoTemplate.updateFirst(query,update,Video.class);
            video = mongoTemplate.findById(comment.getPublishId(),Video.class);
            return video.getCommentCount();
        }else {
            return null;
        }
    }

    @Override
    public Integer delete(Comment comment) {
        //确定删除条件 当前用户的userId 此条评论的publishId和评论类型commentType

        if(comment.getCommentType()<4){
            Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                    .and("publishId").is(comment.getPublishId())
                    .and("commentType").is(comment.getCommentType());
            //从comment表删除该条comment
            mongoTemplate.remove(new Query(criteria), Comment.class);

            //查publish
            Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);

            //将publish表里该条动态的点赞数-1
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(), -1);
            mongoTemplate.updateFirst(query, update, Publish.class);

            publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
            return publish.getLikeCount();
        }else if(comment.getCommentType()==4){
            Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                    .and("publishId").is(comment.getPublishId())
                    .and("commentType").is(comment.getCommentType());
            mongoTemplate.remove(new Query(criteria),Comment.class);
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc(comment.getCol(),-1);
            mongoTemplate.updateFirst(query,update,Comment.class);
            comment = mongoTemplate.findById(comment.getPublishId(), Comment.class);
            return comment.getLikeCount();
        }else if(comment.getCommentType()==5){
            Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                    .and("publishId").is(comment.getPublishId())
                    .and("commentType").is(comment.getCommentType());
            mongoTemplate.remove(new Query(criteria),Comment.class);
            Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();
            update.inc("likeCount",-1);
            mongoTemplate.updateFirst(query,update,Video.class);
            Video video = mongoTemplate.findById(comment.getPublishId(),Video.class);
            return video.getLikeCount();
        }else if(comment.getCommentType() ==6){
            return null;
        }else {
            return null;
        }
    }

    @Override
    public PageResult queryCommentList(Integer page, Integer pagesize, String publishId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(2);

        long count = mongoTemplate.count(new Query(criteria), Comment.class);

        Query query = new Query(criteria)
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageResult(page, pagesize, (int) count, commentList);
    }

    @Override
    public PageResult queryVideoCommentList(Integer page, Integer pagesize, String publishId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(6);

        long count = mongoTemplate.count(new Query(criteria), Comment.class);

        Query query = new Query(criteria)
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageResult(page, pagesize, (int) count, commentList);
    }

    @Override
    public PageResult findByUidAndType(Integer page, Integer pagesize, Long publishUserId, Integer commentType) {
        Criteria criteria = Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType);

        long count = mongoTemplate.count(new Query(criteria), Comment.class);

        Query query = new Query(criteria)
                .skip((page-1)* pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageResult(page,pagesize, (int) count,commentList);
    }
}